$(function () {

    // Select2 by showing the search
    $('.select2-show-search').select2({
        minimumResultsForSearch: ''
    });

    laydate.render({
        elem: '#birthday' //指定元素
    });

    $("input[type='radio'][name='cusType']").click(function () {
        var cusType = $("input[type='radio'][name='cusType']:checked").val();
        if (cusType === "personal") {
            // $("#companyInfo").css('display', 'none');
            $("#sel_company").attr("disabled", "disabled");
        } else if (cusType === "company") {
            // $("#companyInfo").css('display', 'block');
            $("#sel_company").removeAttr('disabled');
        }
    });
    // 客户信息 --- start
    var sel_company_content = "<option label='请选择企业信息'></option>";

    selectAllCompany();
    function selectAllCompany() {
        $.ajax({
            type: "post",
            url: "/crm/company/selList",
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    sel_company_content += "<option value='" + data[i].id + "'>" + data[i].name + "</option>";
                }
                $("#sel_company").html(sel_company_content);
            }
        });
    };

    function cusNameVali() {
        var name = $('input[name="cusName"]');
        var info = $("#cusNameInfo");
        if (name.val().trim() != "") {
            info.html("");
            name.removeClass("is-invalid");
            return true;
        } else {
            name.addClass("is-invalid");
            info.html("请输入正确的企业名称信息！");
            name.focus();
            return false;
        }
    };

    function cusEmailVali() {
        var email = $('input[name="cusEmail"]');
        var info = $("#cusEmailInfo");
        if (email.val().trim() == "" || email.val().match(/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/)) {
            info.html("");
            email.removeClass("is-invalid");
            return true;
        } else {
            email.addClass("is-invalid");
            info.html("请输入正确格式的电子邮箱");
            email.focus();
            return false;
        }
    }

    function idCardVail() {
        var idCard = $('input[name="idCard"]');
        var info = $("#idCardInfo");
        if (idCard.val().trim() == "" || idCard.val().match(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/)) {
            info.html("");
            idCard.removeClass("is-invalid");
            return true;
        } else {
            idCard.addClass("is-invalid");
            info.html("请输入15或18位身份证号");
            idCard.focus();
            return false;
        }
    }

    $("#bt_sub_person").click(function () {
        if (cusNameVali() && cusEmailVali() && idCardVail()) {
            $.ajax({
                type: "post",
                url: "/crm/customer/",
                data: {
                    name: $('input[name="cusName"]').val(),
                    sex: $("input[type='radio'][name='sex']:checked").val(),
                    contact1: $('input[name="contact1"]').val(),
                    contact2: $('input[name="contact2"]').val(),
                    indexes: $('input[name="cusIndexes"]').val(),
                    idCard: $('input[name="idCard"]').val(),
                    birthday: $('input[name="birthday"]').val(),
                    email: $('input[name="cusEmail"]').val(),
                    address: $('input[name="cusAddress"]').val(),
                    need: $('input[name="cusNeed"]').val(),
                    interest: $('input[name="interest"]').val(),
                    impression: $('input[name="impression"]').val(),
                    remarks: $('input[name="cusRemarks"]').val(),
                    companyId: $('#sel_company option:selected').val(),
                    comName: $('#sel_company option:selected').text()
                },
                success: function (data) {
                    if (data == "SUCCESS") {
                        $("#personMsg").css({color: "green"});
                        $("#personMsg").html("客户信息添加成功");
                        window.setTimeout(function () {
                            $("#personMsg").html("");
                        }, 2000);
                    } else {
                        $("#personMsg").css({color: "red"});
                        $("#personMsg").html("客户信息添加失败！");
                        window.setTimeout(function () {
                            $("#personMsg").html("");
                        }, 2000);
                    }
                }

            });
        }
    });

    $("#bt_reset_person").click(function () {
        var person = ["cusName", "contact1", "contact2", "cusIndexes", "idCard", "birthday", "cusEmail", "cusAddress", "cusNeed",
        "interest", "impression", "cusRemarks"];
        resetVal(person, "person");
        $(":radio[name='sex'][value='1']").prop("checked", "checked");
        $(":radio[name='cusType'][value='personal']").prop("checked", "checked");
        $("#sel_company").attr("disabled", "disabled");
        $("#sel_company").find("option[label='请选择企业信息']").attr("selected",true);
        $("#sel_company").html(sel_company_content);
    });

    // 客户信息 --- end


    // 企业信息 --- start
    var sel_invo_content = "<option label='请选择企业发票信息'></option>";

    selectAllInvo();
    function selectAllInvo() {
        $.ajax({
            type: "post",
            url: "/crm/invoice/selList",
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    sel_invo_content += "<option value='" + data[i].id + "'> " + data[i].name + "</option>";
                }
                $("#sel_invo").html(sel_invo_content);
            }
        });
    };

    function comNameVali() {
        var name = $('input[name="companyName"]');
        var info = $("#companyNameInfo");

        info.html("");
        if (name.val().trim() == "") {
            name.addClass("is-invalid");
            info.html("请输入正确的企业名称信息！");
            name.focus();
            return false;
        } else name.removeClass("is-invalid");
        return true;
    };

    $("#bt_sub_company").click(function () {
        if (comNameVali()) {
            $.ajax({
                type: "post",
                url: "/crm/company/",
                data: {
                    name: $('input[name="companyName"]').val(),
                    eamil: $('input[name="comEmail"]').val(),
                    address: $('input[name="comAddress"]').val(),
                    net: $('input[name="net"]').val(),
                    regCap: $('input[name="regCep"]').val(),
                    need: $('input[name="comNeed"]').val(),
                    natrue: $("input[type='radio'][name='nature']:checked").val(),
                    remarks: $('input[name="comRemarks"]').val(),
                    indexes: $('input[name="comIndexes"]').val(),
                    invoiceId: $('#sel_invo option:selected').val()
                },
                success: function (data) {
                    if (data == "SUCCESS") {
                        $("#companyMsg").css({color: "green"});
                        $("#companyMsg").html("企业信息添加成功");
                        window.setTimeout(function () {
                            $("#companyMsg").html("");
                        }, 2000);
                        selectAllCompany();
                    } else {
                        $("#companyMsg").css({color: "red"});
                        $("#companyMsg").html("企业信息添加失败！");
                        window.setTimeout(function () {
                            $("#companyMsg").html("");
                        }, 2000);
                    }
                }

            });
        }
    });

    $("#bt_test").click(function () {
        alert($('#sel_invo option:selected').val());
    });


    $("#bt_reset_company").click(function () {
        alert($('#sel_invo option:selected').val());
        var company = ["companyName", "comAddress", "comIndexes", "nature", "comEmail", "regCep", "comNeed", "net", "comRemarks"];
        resetVal(company, "company");
        $("#sel_invo").find("option[label='请选择企业发票信息']").attr("selected",true);
        $("#sel_invo").html(sel_invo_content);
    });

    // 企业信息 ---end


    // 发票信息 --- start
    function invoNameVali() {
        var name = $('input[name="invoName"]');
        var info = $("#invoNameInfo");

        info.html("");
        if (name.val().trim() == "") {
            name.addClass("is-invalid");
            info.html("请输入正确的发票抬头信息！");
            name.focus();
            return false;
        } else name.removeClass("is-invalid");
        return true;
    };

    function uniqueNoVali() {
        var name = $('input[name="uniqueNo"]');
        var info = $("#uniqueNoInfo");

        info.html("");
        if (name.val().trim() == "" || (name.val().trim().length != 15 && name.val().trim().length != 18 && name.val().trim().length != 20)) {
            name.addClass("is-invalid");
            info.html("请输入正确的纳税人识别号信息！");
            name.focus();
            return false;
        } else name.removeClass("is-invalid");
        return true;
    }

    $("#bt_subInvo").click(function () {
        if (invoNameVali() && uniqueNoVali()) {
            $.ajax({
                type: "post",
                url: "/crm/invoice/",
                data: {
                    "name": $('input[name="invoName"]').val(),
                    "uniqueNo": $('input[name="uniqueNo"]').val(),
                    "address": $('input[name="invoAddress"]').val(),
                    "telephone": $('input[name="invoTelephone"]').val(),
                    "openBank": $('input[name="openBank"]').val(),
                    "openBankNo": $('input[name="openBankNo"]').val(),
                    "remarks": $('input[name="invoRemarks"]').val()
                },
                success: function (data) {
                    if (data == "SUCCESS") {
                        $("#invoMsg").css({color: "green"});
                        $("#invoMsg").html("发票信息添加成功！");
                        window.setTimeout(function () {
                            $("#invoMsg").html("");
                        }, 2000);
                        selectAllInvo();
                    } else {
                        $("#invoMsg").css({color: "red"});
                        $("#invoMsg").html("发票信息添加失败！");
                        window.setTimeout(function () {
                            $("#invoMsg").html("");
                        }, 2000);
                    }
                }
            });
        }
    });

    function resetVal(arr, type) {

        for (var i = 0; i < arr.length; i++) {
            var widget = $('input[name="' + arr[i] + '"]');
            widget.val("");
            widget.removeClass("is-invalid");
            var info = $(".error_info_" + type);
            info.html("");
        }
    }

    $("#bt_reset_invo").click(function () {
        var invo = ["invoName", "uniqueNo", "invoAddress", "invoTelephone", "openBank", "openBankNo", "invoRemarks"];
        resetVal(invo, "invo");
    })

    // 发票信息 --- end

});